package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.FocusBannerBean;
import com.elinkway.infinitemovies.bean.HzAdBean;
import com.elinkway.infinitemovies.bean.HzPayload;
import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bean.Payload;
import com.elinkway.infinitemovies.bean.SDKPayload;
import com.elinkway.infinitemovies.bean.ServerPayload;
import com.elinkway.infinitemovies.bean.TtAdBean;
import com.elinkway.infinitemovies.bean.TtPayload;
import com.elinkway.infinitemovies.bean.XfAdBean;
import com.elinkway.infinitemovies.bean.XfPayload;
import com.elinkway.infinitemovies.bean.YzAdBean;
import com.elinkway.infinitemovies.bean.YzPayload;
import com.elinkway.infinitemovies.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import static com.elinkway.infinitemovies.bean.YzAdBean.AdInfo;
import static com.elinkway.infinitemovies.bean.YzAdBean.AdTracker;
import static com.elinkway.infinitemovies.bean.YzAdBean.DataInfo;

/**
 * Created by huangxiaojian on 2017/5/31.
 *
 * 广告请求数据解析类
 */

public class NewAdDataParser extends LVideoMobileParser<NewAdDataBean>{
    public static final String PROVIDER = "provider";
    public static final String PAYLOAD = "payload";
    public static final String SDK = "sdk";
    public static final String EXT = "ext";

    @Override
    public NewAdDataBean parse(JSONObject data) throws Exception {
        NewAdDataBean newAdDataBean = new NewAdDataBean();
        newAdDataBean.payload = new Payload();
        newAdDataBean.ext = new FocusBannerBean();
        newAdDataBean.ts = getTs();
        String provider = "-1";
        JSONObject payload = null;
        JSONObject ad = null;
        JSONObject focusBanner = null;
        if (null == data || !data.has(SDK)) {
            return null;
        }
        boolean isSdk = data.optBoolean(SDK);
        newAdDataBean.sdk = isSdk;
        if (data.has(EXT)) {
            focusBanner = data.optJSONObject(EXT);
        }

        if (null != focusBanner) {
            newAdDataBean.ext.focusIndex = focusBanner.optString("focusIndex");
            newAdDataBean.ext.badge = focusBanner.optString("badge");
            newAdDataBean.ext.pos = focusBanner.optInt("pos");
        }

        if (data.has(PAYLOAD)) {
            payload = data.optJSONObject(PAYLOAD);
        }
        if (data.has(PROVIDER)) {
            provider = String.valueOf(data.optInt(PROVIDER));
            newAdDataBean.provider = provider;
        }
        if (null != payload && payload.has("ad")) {
            ad = payload.optJSONObject("ad");
        }
        if (isSdk) {
            SDKPayload sdkPayload = new SDKPayload();
            sdkPayload.adId = payload.optString("adId");
            newAdDataBean.payload = sdkPayload;
        } else {
            if (provider.equals(Utils.SERVER_AD)) {
                ServerPayload serverPayload = new ServerPayload();
                serverPayload.title = payload.optString("title");
                serverPayload.desc = payload.optString("desc");
                serverPayload.pic = payload.optString("pic");
                serverPayload.lp = payload.optString("lp");
                serverPayload.action = payload.optInt("action");
                serverPayload.triggerColor = payload.optString("triggerColor");
                serverPayload.triggerText = payload.optString("triggerText");
                serverPayload.triggerTextSpace = payload.optString("triggerTextSpace");
                serverPayload.appName = payload.optString("appName");
                serverPayload.packageName = payload.optString("pkgName");
                newAdDataBean.payload = serverPayload;
            } else if (provider.equals(Utils.YZ_AD)) {
                YzPayload yzPayload = new YzPayload();
                yzPayload.ad = new YzAdBean();
                if (null != ad) {
                    AdInfo adInfo= new AdInfo();
                    adInfo.yzid = ad.optString("yzid");
                    adInfo.html_snippet = ad.optString("html_snippet");
                    adInfo.title = ad.optString("title");
                    adInfo.description = ad.optString("description");
                    adInfo.sub_description = ad.optString("sub_description");
                    adInfo.click_url = ad.optString("click_url");
                    adInfo.deeplink = ad.optString("deeplink");
                    adInfo.app_name = ad.optString("app_name");
                    adInfo.expiration = ad.optString("expiration");
                    adInfo.images = new ArrayList<>();
                    JSONArray images = ad.optJSONArray("images");
                    if (null != images && images.length() > 0) {
                        for (int m = 0; m < images.length(); m++) {
                            JSONObject imageFromServer = images.optJSONObject(m);
                            YzAdBean.ImageInfo imageInfo = new YzAdBean.ImageInfo();
                            imageInfo.url = imageFromServer.optString("url");
                            JSONObject size = imageFromServer.optJSONObject("size");
                            if (null != size) {
                                imageInfo.size.height =  size.optString("height");
                                imageInfo.size.width = size.optString("width");
                            }
                            adInfo.images.add(imageInfo);
                        }
                    }

                    adInfo.icons = new ArrayList<>();
                    JSONArray jsonArrayIcons = ad.optJSONArray("icons");
                    if (null != jsonArrayIcons && jsonArrayIcons.length() > 0) {
                        for (int j = 0; j < jsonArrayIcons.length(); j++) {
                            JSONObject iconsFromServer = jsonArrayIcons.optJSONObject(j);
                            YzAdBean.ImageInfo imageInfo = new YzAdBean.ImageInfo();
                            imageInfo.url = iconsFromServer.optString("url");
                            JSONObject size = iconsFromServer.optJSONObject("size");
                            if (null != size) {
                                imageInfo.size.height =  size.optString("height");
                                imageInfo.size.width = size.optString("width");
                            }
                            adInfo.icons.add(imageInfo);
                        }
                    }

                    adInfo.logos = new ArrayList<>();
                    JSONArray jsonArrayLogos = ad.optJSONArray("logos");
                    if (null != jsonArrayLogos && jsonArrayLogos.length() > 0) {
                        for (int k = 0; k < jsonArrayLogos.length(); k++) {
                            JSONObject imageFromServer = jsonArrayLogos.optJSONObject(k);
                            YzAdBean.ImageInfo imageInfo = new YzAdBean.ImageInfo();
                            imageInfo.url = imageFromServer.optString("url");
                            JSONObject size = imageFromServer.optJSONObject("size");
                            if (null != size) {
                                imageInfo.size.height =  size.optString("height");
                                imageInfo.size.width = size.optString("width");
                            }
                            adInfo.logos.add(imageInfo);
                        }
                    }

                    adInfo.snapshots = new ArrayList<>();
                    JSONArray jsonArraySnapshots = ad.optJSONArray("snapshots");
                    if (null != jsonArraySnapshots && jsonArraySnapshots.length() > 0) {
                        for (int i = 0; i < jsonArraySnapshots.length(); i++) {
                            JSONObject imageFromServer = jsonArraySnapshots.optJSONObject(i);
                            YzAdBean.ImageInfo imageInfo = new YzAdBean.ImageInfo();
                            imageInfo.url = imageFromServer.optString("url");
                            JSONObject size = imageFromServer.optJSONObject("size");
                            if (null != size) {
                                imageInfo.size.height =  size.optString("height");
                                imageInfo.size.width = size.optString("width");
                            }
                            adInfo.snapshots.add(imageInfo);
                        }
                    }

                    adInfo.datas = new ArrayList<>();
                    JSONArray jsonArrayDatas = ad.optJSONArray("datas");
                    if (null != jsonArrayDatas && jsonArrayDatas.length() > 0) {
                        for (int l = 0; l < jsonArrayDatas.length(); l++) {
                            JSONObject jsonObject = jsonArrayDatas.optJSONObject(l);
                            DataInfo dataInfo = new DataInfo();
                            dataInfo.type = jsonObject.optInt("type");
                            dataInfo.value = jsonObject.optString("value");
                            adInfo.datas.add(dataInfo);
                        }
                    }

                    JSONArray trackerArray = ad.optJSONArray("ad_trackers");
                    if(trackerArray != null && trackerArray.length() > 0){
                        ArrayList<AdTracker> trackers = new ArrayList<>();
                        AdTracker tracker = null;
                        for(int i=0; i<trackerArray.length(); i++){
                            JSONObject jsonAdTracker = trackerArray.optJSONObject(i);
                            if (null != jsonAdTracker) {
                                tracker = new AdTracker();
                                tracker.type = jsonAdTracker.optInt("type");
                                tracker.urls = new ArrayList<>();
                                JSONArray jsonArrayUrls = jsonAdTracker.optJSONArray("urls");
                                if (null != jsonArrayUrls && jsonArrayUrls.length() > 0) {
                                    for (int q = 0; q < jsonArrayUrls.length(); q++) {
                                        tracker.urls.add(jsonArrayUrls.optString(q));
                                    }
                                }
                                trackers.add(tracker);
                            }
                        }
                        adInfo.adTrackers = trackers;
                    }

                    adInfo.act_type = ad.optInt("act_type");
                    adInfo.yzid = ad.optString("yzid");
                    yzPayload.ad.adInfo = adInfo;
                    }
                newAdDataBean.payload = yzPayload;
            } else if (provider.equals(Utils.HZ_AD)) {

                HzPayload hzPayload = new HzPayload();
                hzPayload.ad = new HzAdBean();
                if (null != ad) {
                    hzPayload.version = payload.optString("version");
                    hzPayload.ad.sid = ad.optString("sid");
                    hzPayload.ad.mime = ad.optString("mine");
                    hzPayload.ad.aid = ad.optString("aid");
                    hzPayload.ad.duration = ad.optString("duration");
                    hzPayload.ad.src = ad.optString("src");
                    hzPayload.ad.width = ad.optString("width");
                    hzPayload.ad.height = ad.optString("height");

                    hzPayload.ad.title = ad.optString("title");
                    hzPayload.ad.desc = ad.optString("desc");
                    hzPayload.ad.url = ad.optString("url");

                    hzPayload.ad.action = ad.optString("action");
                    hzPayload.ad.price = ad.optString("price");
                    hzPayload.ad.dp_url = ad.optString("dp_url");
                    
                    hzPayload.ad.dp_clk = new ArrayList();
                    JSONArray dpClk = ad.optJSONArray("dp_clk");
                    if (null  != dpClk && dpClk.length() > 0) {
                        for (int q = 0; q < dpClk.length(); q++) {
                            hzPayload.ad.dp_clk.add(dpClk.optString(q));
                        }
                    }

                    hzPayload.ad.ext_urls = new ArrayList();
                    JSONArray extUrls = ad.optJSONArray("ext_urls");
                    if (null  != extUrls && extUrls.length() > 0) {
                        for (int i = 0; i < extUrls.length(); i++) {
                            hzPayload.ad.ext_urls.add(extUrls.optString(i));
                        }
                    }

                    hzPayload.ad.clk = new ArrayList<>();
                    JSONArray clk = ad.optJSONArray("clk");
                    if (null  != clk && clk.length() > 0) {
                        for (int j = 0; j < clk.length(); j++) {
                            hzPayload.ad.clk.add(clk.optString(j));
                        }
                    }
                    JSONArray downloadUrls = ad.optJSONArray("download_urls");
                    hzPayload.ad.download_urls = new ArrayList<>();
                    if (null != downloadUrls && downloadUrls.length() > 0) {
                        for (int k = 0; k < downloadUrls.length(); k++) {
                            hzPayload.ad.download_urls.add(downloadUrls.optString(k));
                        }
                    }

                    JSONArray downloadedUrls = ad.optJSONArray("downloaded_urls");
                    hzPayload.ad.downloaded_urls = new ArrayList<>();
                    if (null != downloadedUrls && downloadedUrls.length() > 0) {
                        for (int m = 0; m < downloadedUrls.length(); m++) {
                            hzPayload.ad.downloaded_urls.add(downloadedUrls.optString(m));
                        }
                    }

                    JSONArray installedUrls = ad.optJSONArray("installed_urls");
                    hzPayload.ad.installed_urls = new ArrayList<>();
                    if (null != installedUrls && installedUrls.length() > 0) {
                        for (int l = 0; l < installedUrls.length(); l++) {
                            hzPayload.ad.installed_urls.add(installedUrls.optString(l));
                        }
                    }

                    JSONObject jsonApp = ad.optJSONObject("app");
                    if(jsonApp != null){
                        hzPayload.ad.app.name = jsonApp.optString("name");
                        hzPayload.ad.app.icon_url = jsonApp.optString("icon_url");
                        hzPayload.ad.app.package_name = jsonApp.optString("package_name");
                        hzPayload.ad.imp = new ArrayList();
                    }
                    JSONObject jsonImp = ad.optJSONObject("imp");
                    JSONArray jsonArrayUrl = null;
                    if (jsonImp != null) {
                        jsonArrayUrl = jsonImp.optJSONArray("0");
                    }
                    if (null != jsonArrayUrl && jsonArrayUrl.length() > 0) {
                        for (int p = 0; p < jsonArrayUrl.length(); p++) {
                            hzPayload.ad.imp.add(jsonArrayUrl.optString(p));
                        }
                    }
                }
                newAdDataBean.payload = hzPayload;
            } else if (provider.equals(Utils.XF_AD)){
                XfPayload xfPayload = new XfPayload();
                xfPayload.ad = new XfAdBean();
                if (null != ad) {
                    xfPayload.ad.adtype = ad.optString("adtype");
                    xfPayload.ad.matype = ad.optString("matype");
                    xfPayload.ad.is_voice_ad = ad.optString("is_voice_ad");
                    xfPayload.ad.batch_cnt = ad.optString("batch_cnt");
                    xfPayload.ad.sessionid = ad.optString("sessionid");
                    JSONArray maArray = ad.optJSONArray("batch_ma");
                    JSONObject maObj = null;
                    if(maArray != null && maArray.length() > 0){
                        maObj = maArray.optJSONObject(0);
                    }
                    if(maObj != null){
                        xfPayload.ad.batch_ma.adtype = maObj.optString("adtype");
                        xfPayload.ad.batch_ma.ad_source_mark = maObj.optString("ad_source_mark");
                        xfPayload.ad.batch_ma.image = maObj.optString("image");
                        xfPayload.ad.batch_ma.icon = maObj.optString("icon");
                        xfPayload.ad.batch_ma.title = maObj.optString("title");
                        xfPayload.ad.batch_ma.sub_title = maObj.optString("sub_title");
                        xfPayload.ad.batch_ma.landing_url = maObj.optString("landing_url");
                        xfPayload.ad.batch_ma.deep_link = maObj.optString("deep_link");
                        xfPayload.ad.batch_ma.package_name = maObj.optString("package_name");

                        xfPayload.ad.batch_ma.img_urls = new ArrayList();
                        JSONArray imgUrls = maObj.optJSONArray("img_urls");
                        if (null  != imgUrls && imgUrls.length() > 0) {
                            for (int i = 0; i < imgUrls.length(); i++) {
                                xfPayload.ad.batch_ma.img_urls.add(imgUrls.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.impr_url = new ArrayList();
                        JSONArray imprUrl = maObj.optJSONArray("impr_url");
                        if (null  != imprUrl && imprUrl.length() > 0) {
                            for (int i = 0; i < imprUrl.length(); i++) {
                                xfPayload.ad.batch_ma.impr_url.add(imprUrl.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.click_url = new ArrayList();
                        JSONArray clickUrl = maObj.optJSONArray("click_url");
                        if (null  != clickUrl && clickUrl.length() > 0) {
                            for (int i = 0; i < clickUrl.length(); i++) {
                                xfPayload.ad.batch_ma.click_url.add(clickUrl.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.inst_installstart_url = new ArrayList();
                        JSONArray installstartUrl = maObj.optJSONArray("inst_installstart_url");
                        if (null  != installstartUrl && installstartUrl.length() > 0) {
                            for (int i = 0; i < installstartUrl.length(); i++) {
                                xfPayload.ad.batch_ma.inst_installstart_url.add(installstartUrl.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.inst_installsucc_url = new ArrayList();
                        JSONArray installsuccUrl = maObj.optJSONArray("inst_installsucc_url");
                        if (null  != installsuccUrl && installsuccUrl.length() > 0) {
                            for (int i = 0; i < installsuccUrl.length(); i++) {
                                xfPayload.ad.batch_ma.inst_installsucc_url.add(installsuccUrl.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.inst_downstart_url = new ArrayList();
                        JSONArray downstartUrl = maObj.optJSONArray("inst_downstart_url");
                        if (null  != downstartUrl && downstartUrl.length() > 0) {
                            for (int i = 0; i < downstartUrl.length(); i++) {
                                xfPayload.ad.batch_ma.inst_downstart_url.add(downstartUrl.optString(i));
                            }
                        }
                        xfPayload.ad.batch_ma.inst_downsucc_url = new ArrayList();
                        JSONArray downsuccUrl = maObj.optJSONArray("inst_downsucc_url");
                        if (null  != downsuccUrl && downsuccUrl.length() > 0) {
                            for (int i = 0; i < downsuccUrl.length(); i++) {
                                xfPayload.ad.batch_ma.inst_downsucc_url.add(downsuccUrl.optString(i));
                            }
                        }

                    }

                }
                newAdDataBean.payload = xfPayload;
            } else if (provider.equals(Utils.TT_AD)) {
                TtPayload ttPayload = new TtPayload();
                ttPayload.ad = new TtAdBean();
                if (null != ad) {
                    ttPayload.ad.request_id = ad.optString("request_id");
                    TtAdBean.Ads currentAds = new TtAdBean.Ads();
                    currentAds.ad_id = ad.optString("ad_id");
                    currentAds.price = ad.optString("price");
                    currentAds.creative = new TtAdBean.MaterialMeta();
                    JSONObject jsonCreative = ad.optJSONObject("creative");
                    if (jsonCreative != null) {
                        currentAds.creative.creative_type = jsonCreative.optInt("creative_type");
                        currentAds.creative.interaction_type = jsonCreative.optString("interaction_type");
                        currentAds.creative.target_url = jsonCreative.optString("target_url");
                        currentAds.creative.download_url = jsonCreative.optString("download_url");
                        currentAds.creative.title = jsonCreative.optString("title");
                        currentAds.creative.description = jsonCreative.optString("description");
                        currentAds.creative.source = jsonCreative.optString("source");
                        currentAds.creative.app_name = jsonCreative.optString("app_name");
                        currentAds.creative.icon = jsonCreative.optString("icon");
                        currentAds.creative.package_name = jsonCreative.optString("package_name");
                        currentAds.creative.win_notice_url = new ArrayList();
                        JSONArray jsonArrayWinUrl = jsonCreative.optJSONArray("win_notice_url");
                        if (jsonArrayWinUrl != null) {
                            for (int j = 0; j < jsonArrayWinUrl.length(); j++) {
                                currentAds.creative.win_notice_url.add(jsonArrayWinUrl.get(j));
                            }
                        }
                        currentAds.creative.show_url = new ArrayList();
                        JSONArray jsonArrayShowUrl = jsonCreative.optJSONArray("show_url");
                        if (jsonArrayShowUrl != null) {
                            for (int k = 0; k < jsonArrayShowUrl.length(); k++) {
                                currentAds.creative.show_url.add(jsonArrayShowUrl.get(k));
                            }
                        }
                        currentAds.creative.click_url = new ArrayList<>();
                        JSONArray jsonArrayClickUrl = jsonCreative.optJSONArray("click_url");
                        if (jsonArrayClickUrl != null) {
                            for (int l = 0; l < jsonArrayClickUrl.length(); l++) {
                                currentAds.creative.click_url.add(jsonArrayClickUrl.get(l));
                            }
                        }
                        currentAds.creative.ext = jsonCreative.optString("ext");
                        currentAds.creative.image_mode = jsonCreative.optInt("image_mode");
                        currentAds.creative.image = new TtAdBean.Image();
                        JSONObject imageList = jsonCreative.optJSONObject("image");
                        if (imageList != null) {
                            currentAds.creative.image.url = imageList.optString("url");
                            currentAds.creative.image.width = imageList.optInt("width");
                            currentAds.creative.image.height = imageList.optInt("height");
                        }
                        currentAds.creative.image_list = new ArrayList<>();
                        JSONArray jsonArrayImageList = jsonCreative.optJSONArray("image_list");
                        if (jsonArrayImageList != null && jsonArrayImageList.length() > 0) {
                            for (int l = 0; l < jsonArrayImageList.length(); l++) {
                                TtAdBean.Image image = new TtAdBean.Image();
                                JSONObject jsonObjImage = (JSONObject) jsonArrayImageList.get(l);
                                if (jsonObjImage == null) {
                                    continue;
                                }
                                image.height = jsonObjImage.optInt("height");
                                image.width = jsonObjImage.optInt("width");
                                image.url = jsonObjImage.optString("url");
                                currentAds.creative.image_list.add(image);
                            }
                        }
                        currentAds.creative.phone_num = jsonCreative.optString("phone_num");
                        currentAds.creative.button_text = jsonCreative.optString("button_text");
                        currentAds.creative.button_text = jsonCreative.optString("button_text");
                    }
                    ttPayload.ad.ads = currentAds;
                    ttPayload.ad.processing_time_ms = ad.optString("processing_time_ms");
                    ttPayload.ad.status_code = ad.optString("status_code");
                    ttPayload.ad.expiration_time = ad.optString("expiration_time");
                }
                newAdDataBean.payload = ttPayload;
            }
        }
        return newAdDataBean;
    }
}
